package com.msblearn.Disruptor.V1;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

import java.nio.ByteBuffer;
import java.util.concurrent.Executors;

public class Main01 {
    public static void main(String[] args) throws InterruptedException {
        LongEventFactory factory = new LongEventFactory();
        //must be power of 2
        int ringBufferSize = 1024;
        Disruptor<LongEvent> disruptor= new Disruptor<LongEvent>(factory,ringBufferSize, Executors.defaultThreadFactory());

        disruptor.handleEventsWith(new LongEventHandler());
        disruptor.start();

        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
        LongEventProducer producer = new LongEventProducer(ringBuffer);

        ByteBuffer bb = ByteBuffer.allocate(8);
        for(long l=0;l<100;l++){
            bb.putLong(0,l);

            producer.onData(bb);

            Thread.sleep(100);
        }
        disruptor.shutdown();
    }
}
